package com.mantou.proxyservice.scheduler;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.mantou.proxyservice.proxeservice.entity.Proxy;
import com.mantou.proxyservice.proxeservice.service.ProxyService;
import com.mantou.proxyservice.proxeservice.service.TesturlService;
import com.mantou.proxyservice.utils.ProxyUtil;

public class Validater implements Runnable {

	@Autowired
	private ProxyService proxyService;
	
	@Autowired
	private TesturlService testurlService;
	
	private ThreadPoolTaskExecutor executor;
	
	private Logger logger = Logger.getLogger(Validater.class);
	
	public ThreadPoolTaskExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(ThreadPoolTaskExecutor executor) {
		this.executor = executor;
	}

	public void run() {
		// TODO Auto-generated method stub
		/*int i =0;
		if(i==0)
			return ;*/
		try {
			List<Proxy> needupdate = proxyService.find4connectionupdate();
			//needupdate.clear();
			if(needupdate.size() ==0)
			{
				logger.info("no proxy need to update");
				System.out.println("no proxy need to update");
				return;
			}
			for(Proxy proxy:needupdate){
				executor.execute(new ProxyTester(proxy));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("error", e);
		}
	}

	private class ProxyTester implements Runnable{
		private Proxy proxy;
		
		public ProxyTester(Proxy proxy) {
			super();
			this.proxy = proxy;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			//now ignore socket and vpn
			if(proxy.getType()== null)
					return;
			try {
				if(ProxyUtil.validateProxyConnect(new HttpHost(InetAddress.getByName(proxy.getIp()), proxy.getPort()))){
					if(proxy.getDirection() <0){
						proxy.setDirection(1);
						proxy.setStability(proxy.getStability() +1);
					}
					if(proxy.getConnectionlevel() <0){
						proxy.setConnectionlevel(proxy.getConnectionlevel() +2);
					}else{
						proxy.setConnectionlevel(proxy.getConnectionlevel() +1);
					}
				}else{
					if(proxy.getDirection() >0){
						proxy.setDirection(-1);
						proxy.setStability(proxy.getStability() +1);
					}
					if(proxy.getConnectionlevel() >0){
						proxy.setConnectionlevel(proxy.getConnectionlevel() -2);
					}else{
						proxy.setConnectionlevel(proxy.getConnectionlevel() -1);
					}
				}
				Proxy updateProxy = new Proxy();
				updateProxy.setConnectionlevel(proxy.getConnectionlevel());
				updateProxy.setId(proxy.getId());
				updateProxy.setDirection(proxy.getDirection());
				updateProxy.setStability(proxy.getStability());
				proxyService.updateByPrimaryKeySelective(updateProxy);
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				proxyService.deleteByPrimaryKey(proxy.getId());
			}
		}
	}
}
